﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Toom.CellularAutomata.Parser
{
	class CaParser
	{

		public CaParser()
		{

		}

		public enum State
		{
			CLEAN,
			WHITESPACE,
			LETTER,
			NUMBER,
			PUNCT,
			NEITHER
		};

		public List<string> eat1(string input)
		{
			List<string> tokens = new List<string>();
			StringBuilder myToken = new StringBuilder();

			char[] inputAsCharArray = input.ToCharArray();
			int len = input.Length;

			State state = State.CLEAN;

			for ( int i=0; i<len; )
			{
				char c = inputAsCharArray[i];

				if (Char.IsWhiteSpace(c))
				{
					if (state == State.CLEAN || state == State.WHITESPACE)
					{
						myToken.Append(c);
						state = State.WHITESPACE;
						i++;
						continue;
					}
					else
					{
						tokens.Add(myToken.ToString());
						myToken.Clear();
						state = State.CLEAN;
						continue;
					}
				}
				else if (Char.IsLetter(c))
				{
					if (state == State.CLEAN || state == State.LETTER)
					{
						myToken.Append(c);
						state = State.LETTER;
						i++;
						continue;
					}
					else
					{
						tokens.Add(myToken.ToString());
						myToken.Clear();
						state = State.CLEAN;
						continue;
					}
				}
				else if (char.IsNumber(c))
				{
					if (state == State.CLEAN || state == State.NUMBER)
					{
						myToken.Append(c);
						state = State.NUMBER;
						i++;
						continue;
					}
					else
					{
						tokens.Add(myToken.ToString());
						myToken.Clear();
						state = State.CLEAN;
						continue;
					}
				}
				else if (char.IsPunctuation(c))
				{
					if (state == State.CLEAN)
					{
						myToken.Append(c);
						state = State.PUNCT;
						i++;
						continue;
					}
					else
					{
						tokens.Add(myToken.ToString());
						myToken.Clear();
						state = State.CLEAN;
						continue;
					}
				}
				else	// neither
				{
					if (state == State.CLEAN || state == State.NEITHER)
					{
						myToken.Append(c);
						state = State.NEITHER;
						i++;
						continue;
					}
					else
					{
						tokens.Add(myToken.ToString());
						myToken.Clear();
						state = State.CLEAN;
						continue;
					}

				}
			}

			tokens.Add(myToken.ToString());

			return tokens;
		}//eat1()

	}//class CaParser
}//ns
